package com.ytkj.digitalConstruction.service.constructionprocess;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.druid.util.StringUtils;
import com.ytkj.digitalConstruction.dao.constructionprocess.ConstructionApproachMapper;
import com.ytkj.digitalConstruction.entity.constructionprocess.ConstructionApproach;
import com.ytkj.digitalConstruction.entity.user.User;
import com.ytkj.digitalConstruction.httpbean.constructionprocess.resbean.WholeFlowInfo;

/**
 * 申请入场Service
 * 
 * @author 宁~
 *
 */
@Service
@Transactional
public class ConstructionApproachService{

	@Autowired
	private ConstructionBaseService constructionBaseService;
	
	@Autowired
	private ConstructionFlowService constructionFlowService;
	
	@Autowired
	private ConstructionApproachMapper constructionApproachMapper;
	
	private Integer setApproachPutMarkId = 6;
	
	private Integer confirmApproach = 8;
	
	private Integer confirmEnterConstruction = 7;
	
	/**
	 * @Description: 设置摆放标识
	 * @Title: setApproachPutMark 
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo setApproachPutMark(Integer flowId, User user) {
		constructionBaseService.checkProcess(flowId, setApproachPutMarkId);
		
		ConstructionApproach approach = new ConstructionApproach();
		approach.setPutMark(1);
		approach.setEntrance(1);
		approach.setFlowId(flowId);
		approach.setStatus(0);
		patchApproach(flowId,approach, user);
		constructionApproachMapper.insertSelective(approach);
		ConstructionUtilService.isAddSuccess(approach.getApplicationId());
		
		return constructionBaseService.processNextResult(flowId, user, 5);
	}
	
	/**
	 * @Description: 指挥部审批完成
	 * @Title: confirmApproach 
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */
	public WholeFlowInfo confirmApproach(Integer flowId, User user){
		constructionBaseService.checkProcess(flowId, confirmApproach);
		
		ConstructionApproach approach = new ConstructionApproach();
		approach.setFlowId(flowId);
		approach.setEntrance(3);
		approach.setStatus(1);
		patchApproach(flowId,approach, user);
		ConstructionUtilService.isUpdateSuccess(constructionApproachMapper.updateByFlowIdSelective(approach));
		
		return constructionBaseService.processNextResult(approach.getFlowId(), user, 7);
	}
	
	/**
	 * @Description: 进入施工区之前安检确认
	 * @Title: confirmEnterConstruction
	 * @param @param flowId
	 * @param @return 
	 * @return WholeFlowInfo
	 * @throws
	 */	
	public WholeFlowInfo confirmEnterConstruction(Integer flowId, User user){
		constructionBaseService.checkProcess(flowId, confirmEnterConstruction);
		
		ConstructionApproach approach = new ConstructionApproach();
		approach.setFlowId(flowId);
		approach.setEntrance(2);
		
		patchApproach(flowId,approach, user);
		ConstructionUtilService.isUpdateSuccess(constructionApproachMapper.updateByFlowIdSelective(approach));
		
		constructionFlowService.processNext(approach.getFlowId());
		
		return constructionBaseService.processNextResult(approach.getFlowId(), user, 6);
	}

	/**
	 * @Description: 补全approach
	 * @Title: patchApproach 
	 * @param @param approach
	 * @param @param user 
	 * @return void
	 * @throws
	 */
	public void patchApproach(Integer flowId,ConstructionApproach approach, User user) {
		
		ConstructionApproach existApproach = constructionApproachMapper.selectByFlowId(flowId);
		if(existApproach != null) {
			approach.setOperator(existApproach.getOperator());
		}
		
		if(StringUtils.isEmpty(approach.getOperator())) {
			approach.setOperator(String.valueOf(user.getUserId()));
		}else {
			approach.setOperator(ConstructionUtilService.synthesisString(approach.getOperator(),",",String.valueOf(user.getUserId())));
		}
		
		approach.setUpdateBy(user.getUserId());
		approach.setUpdateDate(new Date());
	}
}
